package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.config.ThreadPoolConfig;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumItemVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {
    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private CategoryFeignClient categoryFeignClient;

    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private ExecutorService executorService;

    @Override
    public AlbumItemVo loadItemData(Long albumId) {
        AlbumItemVo albumItemVo = new AlbumItemVo();
        CompletableFuture<AlbumInfo> albumFuture = CompletableFuture.supplyAsync(() -> {
            //根据albumId查询专辑信息
            Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "专辑详情页：获取专辑信息失败！");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑详情页：没有对应专辑！");
            albumItemVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, executorService);

        CompletableFuture<Void> userFuture = albumFuture.thenAcceptAsync(albumInfo -> {
            //根据userId查询主播信息
            Result<UserInfo> userInfoResult = userInfoFeignClient.findUserById(albumInfo.getUserId());
            Assert.notNull(userInfoResult, "专辑详情页：获取专辑主播信息失败！");
            UserInfo userInfo = userInfoResult.getData();
            Assert.notNull(userInfo, "专家详情页：专辑没有对应主播！");
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            albumItemVo.setAnnouncer(userInfoVo);
        }, executorService);


        CompletableFuture<Void> categoryFuture = albumFuture.thenAcceptAsync(albumInfo -> {
            //根据三级id查询三级分类
            Result<BaseCategoryView> categoriesResult = categoryFeignClient.findCategories(albumInfo.getCategory3Id());
            Assert.notNull(categoriesResult, "专辑详情页：获取三级分类信息失败！");
            BaseCategoryView baseCategoryview = categoriesResult.getData();
            albumItemVo.setBaseCategoryView(baseCategoryview);
        }, executorService);


        CompletableFuture<Void> statFuture = CompletableFuture.runAsync(() -> {
            //根据albumId查询统计
            Result<AlbumStatVo> albumStatResult = albumInfoFeignClient.findAlbumStat(albumId);
            Assert.notNull(albumStatResult, "专辑详情页：获取专辑统计信息失败！");
            AlbumStatVo albumStatVo = albumStatResult.getData();
            Assert.notNull(albumStatVo, "专辑详情页：专辑没有对应统计信息！");
            albumItemVo.setAlbumStatVo(albumStatVo);
        }, executorService);

        CompletableFuture.allOf(userFuture, categoryFuture, statFuture).join();
        return albumItemVo;
    }
}
